package com.kedou.sdk.params;

import com.kedou.sdk.utils.AESTool;
import com.kedou.sdk.utils.WL;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2017/5/31 0031.
 */

public final class PostRequestParams {

    private static final String TAG = PostRequestParams.class.getSimpleName();

//    final String noncestr;

    final String sign;

//    final long time;

    final JSONObject data;

    private PostRequestParams(final Builder builder) {
        this.sign = builder.sign;
//        this.noncestr = builder.noncestr;
//        this.service = builder.service;
        this.data = builder.data;
    }

    public static PostRequestParams createRequestParams(RequestDataParams data) {

        return new Builder(data).build();
    }

    public String toParamsString() {
        WL.e(TAG, "toParamsString ---");

        try {
            data.put("sign",sign);
//            jsonStr.put("data", data);
//            jsonStr.put("service", service);
//            jsonStr.put("sign", sign);
//            jsonStr.put("time", time);
        } catch (JSONException e) {
            WL.e(TAG, "toParamsString ---> JSONException");
        }
        WL.e(TAG, "toParamsString ---"+ data.toString());
//        RSAUtil.newKey();
//        try {
//            String base = Base64.encodeToString(RSAUtil.encryptByPublicKey(jsonStr.toString().getBytes()),Base64.DEFAULT);
//            WL.e(TAG, "encode ---"+ base);
//            byte[] src = Base64.decode("Ijt7lVvWqNqbqLReC7GR50e3fvqmQdaRB6KylG24Tf2dsxFGIlebwNWEWQYalZkKPkTLgD2n5Pm0YetsEGE6Pykw8lXL3bCXngCsiCDtK0iPpX54tDduj+BaEoRCztrhZHxvLhua6ytA4LNnCGijDWO3OeSGdtVOpS69RKGHQsNCvwZ2E9xF1lvUu8Jjn4/fAvDdroKnaVpMCxe7erUT27UsOmYAGgqWpysBtZyiJLontPOWwt3/X/F+9ZmHCHG5kWZzuzRKirc2KRUZAWjjppn0a3BoG89iqjdTSdP78TD174IFsA3IMd9ty18MPcId91rT2s1zCuisBtvgZsR5xw==",Base64.DEFAULT);
//            byte[] aaa = RSAUtil.decryptByPrivateKey(src);
//            WL.e(TAG, "decode ---"+ new String(aaa));
//            return base.toString();
//        }catch (Exception e){
//            e.printStackTrace();
//            WL.e(TAG, "decode ---"+ "error");
//        }


        return data.toString();
    }

    public static class Builder {

        private String sign;
//        private long time;
        private JSONObject data;
//        private String noncestr;

        public Builder(RequestDataParams data) {

            this.data = data.toJsonObject();
//            this.time = System.currentTimeMillis() / 1000;
//            this.noncestr =  UUID.randomUUID().toString();

//            Log.d("Random string", randomString);
            StringBuilder builder = new StringBuilder()
//                    .append(service).append(time)
                    .append(data.toString());
//                    .append(XmlUtils.getPlatformSecret());
            WL.d(TAG, "Builder sign string: " + builder.toString());
                this.sign = AESTool.encrypt(builder.toString());
            WL.d(TAG, "Builder after sign string: " + sign);
        }

        public PostRequestParams build() {
            return new PostRequestParams(this);
        }

    }
}
